﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using FineUI;
using SDLITS.Model;
using SDLITS.Common;
using MySoft.Data;

namespace SDLITS.Web.SystemManage
{
    public partial class RoleManage : CommonBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnNew.OnClientClick = Window1.GetShowReference("RoleChange.aspx", "新增角色");
                BindGrid(GetWhere());
            }
        }
        private void BindGrid(WhereClip wc)
        {
            Grid1.RecordCount = SDDbSession.Default.From<V_RoleUser>().Where(wc).Count();
            List<V_RoleUser> items = SDDbSession.Default.From<V_RoleUser>().Where(wc).Page(Grid1.PageSize, (Grid1.PageIndex + 1)).ToList();
            Grid1.DataSource = items;
            Grid1.DataBind();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid(GetWhere());
        }
        protected WhereClip GetWhere()
        {
            WhereClip wc = WhereClip.All;
            if (!string.IsNullOrEmpty(ttbxMyBox2.Text))
            {
                wc = wc && V_RoleUser._.RoleName.Contain(ttbxMyBox2.Text.Trim());
            }
            return wc;
        }
        protected void ttbxMyBox2_Trigger2Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(ttbxMyBox2.Text))
            {
                Grid1.PageIndex = 0;
                BindGrid(GetWhere());
                ttbxMyBox2.ShowTrigger1 = true;
            }
            else
            {
                Alert.ShowInTop("请输入你要搜索的关键词！");
            }
        }
        protected void ttbxMyBox2_Trigger1Click(object sender, EventArgs e)
        {
            ttbxMyBox2.Text = "";
            ttbxMyBox2.ShowTrigger1 = false;
            Grid1.PageIndex = 0;
            BindGrid(GetWhere());
        }
        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            int id = int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString());
            if (e.CommandName == "Del")
            {
                SDDbSession.Default.Delete<Role>(Role._.RoleID == id);
                SDDbSession.Default.Delete<RoleInMenu>(RoleInMenu._.RoleID == id);
                BindGrid(GetWhere());

            }
        }

        protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
        {
            //e.Values[5] = "<a style=\"color:#256392;text-decoration:none;display:inline-block;margin:2px;\" href=\"javascript:\" onclick='TabView(\"../SystemManage/RoleChange.aspx?id=" + e.Values[5].ToString() + "\", \"修改" + e.Values[1].ToString() + "\", \"" +e.Values[1].ToString() + "\", \"\");'><span>修改</span></a>";
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            //PageContext.RegisterStartupScript("TabView(\"../SystemManage/RoleChange.aspx\", \"dynamic_tab_addrole\", \"添加角色\", \"\");");
        }
    }
}
